/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase.editpart;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.NodeNameDisplayEditPart;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.diagram.usecase.figure.ActorStringFigure;
import com.cea.papyrus.diagram.usecase.helper.NodeActorHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Property;

/**
 * Controller for a figure 
 */
public class ActorNameDisplayEditPart extends NodeNameDisplayEditPart {


	/**
	 * Create a new ActorNameDisplayEditPart
	 * @param graphnode the graph node associated to this controller
	 * @param modelManager the model manager associated to the uml element
	 */public ActorNameDisplayEditPart(GraphNode graphnode, ModelManager modelManager) {
		super(graphnode,modelManager);
	}

	/**
	 * Create a new ActorNameDisplayEditPart
	 * @param graphnode the graph node associated to this controller
	 * @param element the actor associated to this element
	 * @param modelManager the model manager associated to the uml element
	 */
	public ActorNameDisplayEditPart(GraphNode graphnode,org.eclipse.uml2.uml.Element element,ModelManager modelManager) {
		super(graphnode,element,modelManager);

	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.NodeNameDisplayEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		Dimension size = getFigure().getPreferredSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);
		ActorStringFigure fig = (ActorStringFigure)getFigure();
		
		if (getUmlElement() instanceof NamedElement) {
			fig.setText(((NamedElement)getUmlElement()).getName());
		}

		GraphNode parentGN = (GraphNode)getGraphNode().getSemanticParent();
		if (parentGN != null){
			if (!parentGN.getProperty().isEmpty()) {
				Property propertyAppearance = parentGN.getProperty(NodeHelper.NODE_FIGURE_APPEARANCE);
				if ( propertyAppearance != null ) {
					// Check if presenation of parent is a shape...
					boolean isParentShapeFigure = false;
					if (parentGN.getSemanticModel() != null) {
						isParentShapeFigure = parentGN.getSemanticModel().getPresentation().equals(ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION);
					}
					
					if ( !isParentShapeFigure && propertyAppearance.getValue().equals(NodeActorHelper.ACTOR_STICKMAN_FORM)) {
						fig.refreshColors(parentGN);
						fig.setVisible(true);	
					} else {
						fig.setVisible(false);
					}		
				}
			}
		}

		((GraphicalEditPart) getParent()).setLayoutConstraint(this,fig,r);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.NodeNameDisplayEditPart#createFigure()
	 */
	@Override
	protected ActorStringFigure createFigure() {
		return new ActorStringFigure(((NamedElement)getUmlElement()).getName());
	}
}